﻿/// <reference path="../knockout-2.1.0.debug.js" />
/// <reference path="../knockout.mapping-latest.debug.js" />
/// <reference path="../toastr.js" />

{
    function dateFromJson(str) {
        var match;
        if (!(match = str.match(/\d+/))) {
            return false;
        }
        var date = new Date();
        date.setTime(match[0] - 0);
        return date;
    }

    Date.prototype.toDate = function () {
        var date = this;

        var dd = date.getDate() + 1;
        var mm = date.getMonth();
        var yyyy = date.getFullYear();
        return dd + '/' + mm + '/' + yyyy;
    };

    Date.prototype.toTime = function () {
        var date = this;

        var str = date.toDate();
        var hh = date.getHours();
        var mm = date.getMinutes();
        var ss = date.getSeconds();

        str += ' ' + hh + ':' + mm + ':' + ss;
        return str;
    };
}

var templateViewModel = function(rawData) {
    var self = this;

    var emptyModel = {};
    rawData = $.extend({}, emptyModel, rawData);
    
    var mappingOptions = { };
    ko.mapping.fromJS(rawData, mappingOptions, self);

    return self;
};

var MessageModel = function () {
    var self = this;

    self.time = "";
    self.type = "";
    self.content = "";

    return self;
};


var AdditionalQuestionModel = function(rawData) {
    var self = this;
    var emptyModel = {
        Id: null,
        Description: null
    };

    rawData = $.extend({}, emptyModel, rawData);

    var mappingOptions = {};
    ko.mapping.fromJS(rawData, mappingOptions, self);
    
    return self;
};

var AddonModel = function (rawData) {
    var self = this;

    var emptyModel = {
        Id: null,
        Quantity: null,
        Code: null,
        Description: null,
        Price: null
    };
    
    rawData = $.extend({}, emptyModel, rawData);

    var mappingOptions = {};
    ko.mapping.fromJS(rawData, mappingOptions, self);

    return self;
};

var LineItemViewModel = function (rawData) {
    var self = this;

    var emptyModel = {
        Id: null,
        Select: null,
        Code: null,
        Quantity: null,
        Description: null,
        Width: null,
        Height: null,
        Depth: null,
        Hinge: null,
        FE: null,
        AdditionalQuestions: null,
        Addons: null
    };

    rawData = $.extend({},emptyModel,rawData);

    var mappingOptions = {
        AdditionalQuestions: {
            create: function(option) {
                return new AdditionalQuestionModel(option.data);
            }
        },
        Addons: {
            create: function(option) {
                return new AddonModel(option.data);
            }
        }
    };
    
    ko.mapping.fromJS(rawData, mappingOptions, self);


    //helper properties
    self.isEdit = ko.observable();

    self.isExpand = ko.observable(false);

    //computed property
    self.textIndicator = ko.computed(function () {
        if (self.isExpand()) {
            return '˅';
        }
        return '˃';
    });

    self.haveAdditionalQuestionNOT = ko.computed(function () {
        return self.AdditionalQuestions().length == 0;
    });

    self.haveAddonNOT = ko.computed(function () {
        return self.Addons().length == 0;
    });

    //event handler
    
    self.toggleLine = function () {
        self.isExpand(!self.isExpand());
        return false;
    };

    self.nextClick = function () {
        toastr.error("not implemented");
        return false;
    };

    self.lineCompareClick = function () {
        toastr.error("not implemented");
        return false;
    };

    self.editClick = function () {
        toastr.error("not implemented");
        return false;
    };

    return self;
};

var GridViewViewModel = function (rawData) {
    var self = this;

    var mappingOptions = {
        LineItems: {
            create: function(options) {
                return new LineItemViewModel(options.data);
            }
        }
    };

    ko.mapping.fromJS(rawData, mappingOptions, self);


    //event handler
    self.addLineItem = function () {
        toastr.error("not implemented");
        return false;
    };

    self.save = function() {
        var data = ko.toJSON( self.LineItems());
        repository.lineitem.save(data);
        return false;
    };

    self.reset = function () {
        
        var data = {
            LineItems: repository.lineitem.getAll()
        };

        ko.mapping.fromJS(data, self);

        logger.warning("Refresh data");
        return false;
    };
    
    return self;
}